package com.simple.datax.flow.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
	public static String defaultDayPattern = "yyyyMMdd";
	public static String curTimePattern = "yyyyMMddhhmmss";

	public static final String getDate(Date aDate) {
		SimpleDateFormat df = null;
		String returnValue = "";
		if (aDate != null) {
			df = new SimpleDateFormat(defaultDayPattern);
			returnValue = df.format(aDate);
		}

		return (returnValue);
	}
	
	/**
	 * 获取当天日期
	 * @return
	 */
	public static final String getToday() {
		SimpleDateFormat df = null;
		Date aDate = new Date();
		String returnValue = "";
		df = new SimpleDateFormat(defaultDayPattern);
		returnValue = df.format(aDate);
		return (returnValue);
	}
	
	/**
	 * 获取当前时间
	 * @return
	 */
	public static final String getCurTime() {
		SimpleDateFormat df = null;
		Date aDate = new Date();
		String returnValue = "";
		df = new SimpleDateFormat(curTimePattern);
		returnValue = df.format(aDate);
		return (returnValue);
	}


	/**
	 * 将字符串转换为日期
	 * @param aMask
	 * @param strDate
	 * @return
	 * @throws ParseException
	 */
	public static Date convertStringToDate(String aMask, String strDate)
			throws ParseException {
		SimpleDateFormat df = null;
		Date date = null;
		if (aMask != null && !aMask.equals("") && strDate != null
				&& !strDate.equals("")) {
			df = new SimpleDateFormat(aMask);
			date = df.parse(strDate);
		}

		return (date);
	}

	/**
	 * 两个日期相差天数
	 * @param fDate
	 * @param oDate
	 * @return
	 */
	public static int getIntervalDays(Date fDate, Date oDate) {
       if (null == fDate || null == oDate) {
           return -1;
       }
       long intervalMilli = oDate.getTime() - fDate.getTime();
       return (int) (intervalMilli / (24 * 60 * 60 * 1000));
    }
}
